package com.hongfeng.support.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.item.tag.LogicOperator;
import com.hongfeng.support.model.BuyInfoType;
import com.hongfeng.support.service.BuyInfoTypeService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 
 * <供求类型Controller>
 * 
 * @author llc
 * @date 创建时间：2012-12-5 下午02:26:49
 */
@Controller
@Online
public class BuyInfoTypeController extends BaseController {

	@Resource
	private BuyInfoTypeService buyInfoTypeService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String name, Page<BuyInfoType> page) {
		log.info("供求类型：" + name);
		ParmItem item = new ParmItem("name", name, FieldOperator.LIKE);
		buyInfoTypeService.findPageByQuery(page, BuyInfoType.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("listAll")
	public void listAll(HttpServletRequest request, HttpServletResponse response) {
		List<BuyInfoType> types = buyInfoTypeService.findAll();
		String json = JsonUtils.array2Json(types);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("buyInfoType") String buyInfoTypeJson) {
		log.info("添加供求类型:" + (buyInfoTypeJson != null ? buyInfoTypeJson : null));
		if (buyInfoTypeJson == null) {
			this.sendErrorResponse(response, "类型名称不能为空。");
			return;
		}
		BuyInfoType buyInfoType = (BuyInfoType) JsonUtils.json2Object(
				buyInfoTypeJson, BuyInfoType.class);
		
		List<ParmItem> items = new ArrayList<ParmItem>(2);
		items.add(new ParmItem("name", buyInfoType.getName(),
				FieldOperator.EQUAL, LogicOperator.AND));
		items.add(new ParmItem("code", buyInfoType.getCode(),
				FieldOperator.EQUAL, LogicOperator.OR));
		List<BuyInfoType> list = buyInfoTypeService.findAllByQuery(
				BuyInfoType.class, items);
		
		if (list == null || list.size() == 0) {
			buyInfoTypeService.create(buyInfoType);
		} else {
			this.sendErrorResponse(response, "该类型名称或编码已存在。");
		}
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("buyInfoType") String buyInfoTypeJson) {
		log.info("修改供求类型:" + (buyInfoTypeJson != null ? buyInfoTypeJson : null));
		if (buyInfoTypeJson == null) {
			this.sendErrorResponse(response, "类型名称不能为空。");
			return;
		}
		BuyInfoType buyInfoType = (BuyInfoType) JsonUtils.json2Object(
				buyInfoTypeJson, BuyInfoType.class);
		BuyInfoType qBuyInfoType = buyInfoTypeService.read(buyInfoType.getId());

		boolean nameEquals = buyInfoType.getName().equals(
				qBuyInfoType.getName());
		boolean codeEquals = buyInfoType.getCode().equals(
				qBuyInfoType.getCode());
		if (!nameEquals || !codeEquals) {
			List<ParmItem> items = new ArrayList<ParmItem>(2);
			items.add(new ParmItem("name", buyInfoType.getName(),
					FieldOperator.EQUAL, LogicOperator.AND));
			items.add(new ParmItem("code", buyInfoType.getCode(),
					FieldOperator.EQUAL, LogicOperator.OR));
			List<BuyInfoType> list = buyInfoTypeService.findAllByQuery(
					BuyInfoType.class, items);

			if (list == null || list.size() == 0) {
				buyInfoType.setCreateTime(qBuyInfoType.getCreateTime());
				buyInfoType.setCreator(qBuyInfoType.getCreator());
				buyInfoTypeService.update(buyInfoType);
			} else {
				this.sendErrorResponse(response, "该类型名称或编码已存在。");
			}
		}
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) {
		log.info("读取供求类型:" + id);
		BuyInfoType buyInfoType = buyInfoTypeService.read(id);
		String json = JsonUtils.bean2Json(buyInfoType);
		log.info("供求类型：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除供求类型:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的供求类型。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			buyInfoTypeService.delete(Long.parseLong(id));
		}
	}
}
